﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class ScrappedWorkService : BaseService, IScrappedWorkService
    {
        private readonly IScrappedWorkRepository _scrappedWorkRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ScrappedWorkService(IMapper mapper, IScrappedWorkRepository scrappedWorkRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _scrappedWorkRepository = scrappedWorkRepository;
            _unitOfWork = unitOfWork;
        }

        public bool AddOrModefiyScrappedWork(ScrappedWork model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _scrappedWorkRepository.UpdEntity(model);
            }
            else
            {
                _scrappedWorkRepository.AddEntity(model);

            }
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelScrappedWork(long scrappedWorkId)
        {
            var Quote = _scrappedWorkRepository.QueryEntityById(scrappedWorkId);
            if (Quote == null)
            {
                return false;
            }
            _scrappedWorkRepository.DelEntity(Quote);
            return _unitOfWork.SaveChangesEntity();
        }

        public ScrappedWork? GetScrappedWork(long scrappedWorkId)
        {
            return _scrappedWorkRepository.QueryEntityById(scrappedWorkId);
        }

        public List<ScrappedWork> GetScrappedWorkData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _scrappedWorkRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.EquipmentName.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
    }
}
